﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;

namespace QuanLyPhongKham
{
    public partial class ucDiUngThuoc : XtraUserControl
    {
        private DataTable dt = new DataTable();
        private string sql = "";
        private DAL.AccessData m = new DAL.AccessData();
        public ucDiUngThuoc(string mabn,string hoten,string namsinh)
        {
            InitializeComponent();
            this.mabn.Text = mabn;
            this.hoten.Text = hoten;
            this.namsinh.Text = namsinh;
        }

        private void butMoi_Click(object sender, EventArgs e)
        {
            mahc.Text = ""; tenhc.Text = ""; mucdo.SelectedIndex = 0;
            tenhc.Enabled = true; mucdo.Enabled = true;
            tenhc.Focus();
            ena_button(false);
        }
        private void load_grid()
        {
            sql = "select a.mahc,b.tenhc,'Độ '||''||a.mucdo as mucdo from " + m.user + ".d_diungthuoc a," + m.user + ".d_dmhoatchat b," + m.user + ".btdbn c where a.mabn=c.mabn and a.mahc=b.mahc and a.mabn='" + mabn.Text + "' order by b.tenhc";
            dt = m.get_data(sql).Tables[0];
            dataGrid1.DataSource = dt;
        }
        private void AddGridTableStyle()
        {
            DataGridColoredTextBoxColumn TextCol;
            delegateGetColorRowCol de = new delegateGetColorRowCol(MyGetColorRowCol);
            DataGridTableStyle ts = new DataGridTableStyle();
            ts.MappingName = dt.TableName;
            ts.AlternatingBackColor = Color.Linen;
            ts.BackColor = Color.GhostWhite;
            ts.ForeColor = Color.MidnightBlue;
            ts.GridLineColor = SystemColors.Control;
            ts.HeaderBackColor = SystemColors.Control;
            ts.HeaderForeColor = Color.Navy;
            ts.SelectionBackColor = Color.Teal;
            ts.SelectionForeColor = Color.White;
            ts.ReadOnly = true;
            ts.RowHeaderWidth = 5;

            TextCol = new DataGridColoredTextBoxColumn(de, 0);
            TextCol.MappingName = "mahc";
            TextCol.HeaderText = "Mã HC";
            TextCol.Width = 60;
            ts.GridColumnStyles.Add(TextCol);
            dataGrid1.TableStyles.Add(ts);

            TextCol = new DataGridColoredTextBoxColumn(de, 1);
            TextCol.MappingName = "tenhc";
            TextCol.HeaderText = "Tên hoạt chất";
            TextCol.Width = 410;
            ts.GridColumnStyles.Add(TextCol);
            dataGrid1.TableStyles.Add(ts);

            TextCol = new DataGridColoredTextBoxColumn(de, 2);
            TextCol.MappingName = "mucdo";
            TextCol.HeaderText = "Mức độ";
            TextCol.Width = 70;
            ts.GridColumnStyles.Add(TextCol);
            dataGrid1.TableStyles.Add(ts);
        }
        public Color MyGetColorRowCol(int row, int col)
        {
            Color c = Color.Navy;
            try
            {
                if (row == dataGrid1.CurrentRowIndex)
                {
                    dataGrid1.Select(row);
                    c = Color.White;
                }
            }
            catch
            {
            }
            return c;
        }
        public delegate Color delegateGetColorRowCol(int row, int col);
        public class DataGridColoredTextBoxColumn : DataGridTextBoxColumn
        {
            private delegateGetColorRowCol _getColorRowCol;
            private int _column;
            public DataGridColoredTextBoxColumn(delegateGetColorRowCol getcolorRowCol, int column)
            {
                _getColorRowCol = getcolorRowCol;
                _column = column;
            }
            protected override void Paint(System.Drawing.Graphics g, System.Drawing.Rectangle bounds, System.Windows.Forms.CurrencyManager source, int rowNum, System.Drawing.Brush backBrush, System.Drawing.Brush foreBrush, bool alignToRight)
            {
                try
                {
                    foreBrush = new SolidBrush(_getColorRowCol(rowNum, this._column));
                }
                catch { }
                finally
                {
                    base.Paint(g, bounds, source, rowNum, backBrush, foreBrush, alignToRight);
                }
            }
        }
        private void ena_button(bool ena)
        {
            butMoi.Enabled = ena;
            butLuu.Enabled = !ena;
            butSua.Enabled = ena;
            butHuy.Enabled = ena;
            butBoqua.Enabled = !ena;
            butKetthuc.Enabled = ena;
        }
        
        private void frmDiungthuoc_Load(object sender, EventArgs e)
        {
            listhchat.DisplayMember = "MAHC";
            listhchat.ValueMember = "TENHC";
            listhchat.DataSource = m.get_data("select mahc,tenhc from " + m.user + ".d_dmhoatchat order by tenhc").Tables[0];
            load_grid();
            AddGridTableStyle();
            tenhc.Focus();
        }

        private void dataGrid1_CurrentCellChanged(object sender, EventArgs e)
        {
            reft_text();
            tenhc.Enabled = false; mucdo.Enabled = false;
            ena_button(true);
        }   
        private void reft_text()
        {
            try
            {
                mahc.Text = dataGrid1[dataGrid1.CurrentCell.RowNumber, 0].ToString();
                tenhc.Text = dataGrid1[dataGrid1.CurrentCell.RowNumber, 1].ToString();
                mucdo.SelectedIndex = int.Parse(dataGrid1[dataGrid1.CurrentCell.RowNumber, 2].ToString().Substring(dataGrid1[dataGrid1.CurrentCell.RowNumber, 2].ToString().Length - 1)) - 1;
            }
            catch { }
        }
        private void tenhc_TextChanged(object sender, EventArgs e)
        {
            if (this.ActiveControl == tenhc)
            {
                try
                {
                    Filt_nhom(tenhc.Text);
                    listhchat.BrowseToBtdkp(tenhc, mahc, mucdo);
                }
                catch { }
            }
        }
        private void Filt_nhom(string ten)
        {
            try
            {
                CurrencyManager cm = (CurrencyManager)BindingContext[listhchat.DataSource];
                DataView dv = (DataView)cm.List;
                dv.RowFilter = "tenhc like '%" + ten.Trim() + "%'";
            }
            catch { }
        }
        private void butBoqua_Click(object sender, EventArgs e)
        {
            load_grid();
            reft_text();
            ena_button(true);
        }

        private void butLuu_Click(object sender, EventArgs e)
        {
            if (mahc.Text == "" || tenhc.Text == "") return;
            m.upd_diungthuoc(mabn.Text, mahc.Text, mucdo.SelectedIndex + 1);
            load_grid();
            tenhc.Enabled = false; mucdo.Enabled = false;
            ena_button(true); reft_text();
        }

        private void butSua_Click(object sender, EventArgs e)
        {
            tenhc.Enabled = true; mucdo.Enabled = true;
            ena_button(false);
            tenhc.Focus();
        }

        private void butHuy_Click(object sender, EventArgs e)
        {
            if (mahc.Text == "") return;
            if (MessageBox.Show("Bạn có đồng ý huỷ không?", DAL.AccessData.Msg, MessageBoxButtons.OKCancel, MessageBoxIcon.Question) == DialogResult.OK)
            {
                m.execute_data("delete from " + m.user + ".d_diungthuoc where mabn='" + mabn.Text + "' and mahc='" + mahc.Text + "'");
                load_grid();
                reft_text();
            }
        }

        private void butKetthuc_Click(object sender, EventArgs e)
        {
            //fix:
            //this.Close();
        }

        private void tenhc_Validated(object sender, EventArgs e)
        {
            if (!listhchat.Focused) listhchat.Hide();
        }

        private void tenhc_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Down || e.KeyCode == Keys.Up) listhchat.Focus();
            else if (e.KeyCode == Keys.Enter)
            {
                if (listhchat.Visible) listhchat.Focus();
                else SendKeys.Send("{Tab}{Home}");
            }
        }

        private void mucdo_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
                SendKeys.Send("{Tab}");
        }   
    }
}